<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */

//Place code here

global $db,$table_name;
?>
<form action="?mod=core&file=createclass" method="post">
    Table Name : 

    <select name="table_name">
<?php
    $sql = "SHOW TABLES FROM helpdeskdb";
$result = $db->query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
 
while ($row = $db->fetch_array($result)) {
    extract($row);
    if ($Table_in_helpdeskdb==$table_name){
      echo "<option value=\"$Tables_in_helpdeskdb\" selected>$Tables_in_helpdeskdb</option>\n";  
    } else {
    echo "<option value=\"$Tables_in_helpdeskdb\">$Tables_in_helpdeskdb</option>\n";
    }
}


    ?>
        </select>
    <input type="submit" value="Kirim"/>
    
</form>
<?php
if (!empty($table_name)){
    global $db,$table_name;
    $sql="select * from $table_name";
    $result=$db->query($sql);
    $nf=mysql_num_fields($result);
    $html="<table>\r\n";
    $vars="";
    $cast_variable="";
    $insert="insert into $table_name values (null,";
    $update="update $table_name set ";
    for ($i=0;$i<$nf;$i++){
        $meta=mysql_fetch_field($result, $i);
        $vars.="var $".$meta->name.";\r\n";
        
        if ($i>0){
            $insert.="'{'$'this->".$meta->name."}',\r\n";
            $update.="$meta->name='{'$'this->".$meta->name."}',\r\n";
            $cast_variable.="$'this->$meta->name=$".$meta->name.";\r\n";
        }
    }
    $insert.=")";
    $update.=" where id=\".$'this->id";
    $html.="<tr><td></td><td><input type=\\\"submit\\\" value=\\\"Kirim\\\"/></tr></table>";
}
$class_name=ucwords($table_name);
$class_name=str_replace("_", '', $class_name);
$sql_open_id="select * from $table_name where id=$"."id";
$sql_open_all="select * from $table_name order by id asc";
$class_template =
"class $class_name {
    $vars\r\n
    function open(){
        global $'db;
        $'sql=\"$sql_open_id\";
        $'row=$'db->fetch_row($'sql);
        if (!empty('$'row)){
            extract($'row);
            $cast_variable
        }
    }\r\n
    function openAll(){
        $'sql=\"$sql_open_all\";
        $'result=$'db->query($'sql);
        return $'db->fetch_array($'result);
        
    }\r\n
    function insert(){
    global $'db;
    $'sql=\"$insert\";
    $'db->query($'sql);
    return mysql_insert_id();
    }\r\n
    function update(){
        global $'db;
        $'sql=\"$update;
        $'db->query($'sql);
    }\r\n
    function delete(){
        global $'db;
        $'sql=\"delete from $table_name where id=\".$'this->id;
        $'db->query($'sql);
    }\r\n
}";
$class_template=str_replace("$'", "$", $class_template);
$class_template=str_replace("'{'", "'{", $class_template);
echo "<textarea rows=20 cols=60>$class_template</textarea>";

?>